package cn.edu.scau.nursingcenter.controller;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.scau.nursingcenter.bean.ResultBean;
import cn.edu.scau.nursingcenter.entiry.Customer;
import cn.edu.scau.nursingcenter.service.impl.CustomerServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;



/**
  * 客户控制器类CustomerController
 * @author feng
 *
 */
@Api(tags = "客户控制器类")
@RestController
@RequestMapping(value = "customer")
public class CustomerController {
	
	@Autowired
	private CustomerServiceImpl customerServiceImpl;
	
	/**
	  * 显示所有客户信息
	 * @return
	 */
	@ApiOperation(value = "显示所有客户信息")
	@GetMapping(value = "/list")
	@ResponseBody
	public ResultBean list() {
		
		List<Customer> customers = customerServiceImpl.selectALL();
		ResultBean resultBean = new ResultBean(200,true,"显示所有客户信息成功",customers);
		return resultBean;
	}
	/**
	 * 新增客户信息
	 * @param customer
	 * @return
	 */
	@ApiOperation(value = "新增客户信息")
	@PostMapping(value = "/insert")
	@ResponseBody
	public ResultBean insert(@Validated @RequestBody Customer customer) {
		ResultBean resultBean = null;
		Customer targetCustomer = customerServiceImpl.insert(customer);
		if(targetCustomer != null) {
			resultBean = new ResultBean(200, true, "客户入住成功", targetCustomer);	
		}else {
			resultBean = new ResultBean(5008, false, "客户入住失败", null);
		}
		return resultBean;
	}
	/**
	 * 根据客户编号查询
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据客户编号查询")
	@ApiImplicitParam(name = "id",value = "编号",required = true,dataType = "int")
	@GetMapping(value = "/findById")
	@ResponseBody
	public ResultBean findById(Integer id) {
		ResultBean resultBean = null;
		Customer targetCustomer = customerServiceImpl.findById(id);
		if(targetCustomer != null) {
			resultBean = new ResultBean(200,true,"查询成功",targetCustomer);
		}else {
			resultBean = new ResultBean(5009,false,"查询失败",null);
		}
		return resultBean;
	}

}
